package controllers

import (
	"github.com/beego/beego/v2/server/web"
	"rate/common/consts"
	"rate/common/i18n"
)

type ErrorController struct {
	web.Controller
}

type result struct {
	Code string
	Msg  string
}

func (err *ErrorController) Error500() {
	err.Data["json"] = result{
		Code: consts.SystemError,
		Msg:  i18n.I(consts.SystemError, cl(err)),
	}
	err.ServeJSON()
}

func (err *ErrorController) Error401() {
	err.Data["json"] = result{
		Code: consts.Permission,
		Msg:  i18n.I(consts.Permission, cl(err)),
	}
	err.ServeJSON()
}

func (err *ErrorController) Error402() {
	err.Data["json"] = result{
		Code: consts.UnLogin,
		Msg:  i18n.I(consts.UnLogin, cl(err)),
	}
	err.ServeJSON()
}

func (err *ErrorController) Error4000() {
	err.Data["json"] = result{
		Code: consts.ParamError,
		Msg:  i18n.I(consts.ParamError, cl(err)),
	}
	err.ServeJSON()
}

func (err *ErrorController) Error4001() {
	err.Data["json"] = result{
		Code: consts.RepeatName,
		Msg:  i18n.I(consts.RepeatName, cl(err)),
	}
	err.ServeJSON()
}

// cl 获取会员语言
func cl(err *ErrorController) string {
	session := err.GetSession("cl")
	cl := "zh"
	if session != nil {
		cl = session.(string)
	}
	return cl
}
